package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.nursing.service.WeChatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 怡宝
 */
@Service
public class WeChatServiceImpl implements WeChatService {
    private static final String OPENID_ID_URL = "https://api.weixin.qq.com/sns/jscode2session";
    private static final String ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";
    private static final String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={}";

    @Value("${wechat.AppID}")
    private String appId;
    @Value("${wechat.AppSecret}")
    private String appSecret;


    /**
     * 获取opeID
     *
     * @param code
     * @return
     */
    @Override
    public String getOpenId(String code) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", appId);
        paramMap.put("secret", appSecret);
        paramMap.put("js_code", code);
        paramMap.put("grant_type", "authorization_code");
        HttpResponse response = HttpUtil.createGet(OPENID_ID_URL).form(paramMap).timeout(5000).execute();
        if (response.isOk()) {
            String result = response.body();
            JSONObject jsonObject = JSONUtil.parseObj(result);
            String openid = jsonObject.getStr("openid");
            return openid;
        }
        throw new ServiceException("获取openid失败，请重试");
    }

    /**
     * 获取手机号
     *
     * @param phoneCode
     */
    @Override
    public String getPhone(String phoneCode) {
        String accessToken = getAccessToken();
        String url = StrUtil.format(PHONE_URL, accessToken);
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("code",phoneCode);
        HttpResponse response = HttpUtil.createPost(url).body(JSONUtil.toJsonStr(paramMap)).timeout(5000).execute();
        if (response.isOk()) {
            String body = response.body();
            JSONObject entries = JSONUtil.parseObj(body);
            String phone = entries.getJSONObject("phone_info").getStr("purePhoneNumber");
            return phone;
        }
        throw new ServiceException("获取手机号失败，请重试");
    }

    public String getAccessToken() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("grant_type", "client_credential");
        paramMap.put("appId", appId);
        paramMap.put("secret", appSecret);
        HttpResponse response = HttpUtil.createGet(ACCESS_TOKEN).form(paramMap).timeout(5000).execute();
        if (response.isOk()) {
            String body = response.body();
            JSONObject entries = JSONUtil.parseObj(body);
            String accessToken = entries.getStr("access_token");
            return accessToken;
        }
        throw new ServiceException("获取access_token失败，请重试");
    }
}
